package com.spring.boot.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 */
//@Component//没有redis，先注释掉
public class RedisCountInterceptor implements HandlerInterceptor{

    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();


        BoundHashOperations<String, Object, Object> stringObjectObjectBoundHashOperations = redisTemplate.boundHashOps("aaa");
//        stringObjectObjectBoundHashOperations.
//        ValueOperations<String, String> operations = redisTemplate.opsForValue();
//        operations.

        // 默认每次访问uri就会+1
        redisTemplate.opsForValue().increment(uri);

        return true;
    }
}
